package com.javafactroy.kunlong.test.engine;

import com.javafactroy.kunlong.DbAgent;
import com.javafactroy.kunlong.framework.ruleengine.DroolsService;
import com.javafactroy.kunlong.framework.ruleengine.OrderFact;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.extension.ExtendWith;
import org.kie.api.KieServices;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;

//https://zhuanlan.zhihu.com/p/515986392
@SpringBootTest(classes = {DbAgent.class}, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ExtendWith(SpringExtension.class)
@Slf4j
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@TestMethodOrder(MethodOrderer.MethodName.class)
//https://www.nowcoder.com/
//https://leetcode.cn/problemset/all/
public class DroolsTest {

    DroolsService droolsService;

    @Test
    public void test001_DroolsWith7_6_0_final() {
        // 获取 KieServices
        KieServices kieServices = KieServices.Factory.get();
        // 获取 kieContainer
        KieContainer kieContainer = kieServices.newKieClasspathContainer();
        // 获取 kieSession
        KieSession kieSession = kieContainer.newKieSession();
        // 写入 fact对象
        OrderFact fact = new OrderFact();
        fact.setPrice(150d);
        kieSession.insert(fact);
        // 激活规则,由drools自行匹配, 匹配成功则执行规则
        kieSession.fireAllRules();
        // 关闭session
        kieSession.dispose();
        System.out.println("折后价格: " + fact.getDiscountPrice());
    }

    @Test
    public void test002_DroolsWith7_6_0_final() {

        // 写入 fact对象
        OrderFact fact = new OrderFact();
        fact.setPrice(150d);
        DroolsService.fire(fact);
        System.out.println("折后价格: " + fact.getDiscountPrice());
    }
}
